home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_kernel_source / KERNEL / EXIT.C < prev    next >
C/C++ Source or Header  |  1999-09-17  |  12KB  |  506 lines

  1. /*
  2.  *  linux/kernel/exit.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  */
  6.  
  7. #include <linux/config.h>
  8. #include <linux/malloc.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/module.h>
  12. #ifdef CONFIG_BSD_PROCESS_ACCT
  13. #include <linux/acct.h>
  14. #endif
  15.  
  16. #include <asm/uaccess.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/mmu_context.h>
  19.  
  20. extern void sem_exit (void);
  21. extern struct task_struct *child_reaper;
  22.  
  23. int getrusage(struct task_struct *, int, struct rusage *);
  24.  
  25. static void release(struct task_struct * p)
  26. {
  27.     if (p != current) {
  28. #ifdef __SMP__
  29.         /*
  30.          * Wait to make sure the process isn't active on any
  31.          * other CPU
  32.          */
  33.         for (;;)  {
  34.             int has_cpu;
  35.             spin_lock(&scheduler_lock);
  36.             has_cpu = p->has_cpu;
  37.             spin_unlock(&scheduler_lock);
  38.             if (!has_cpu)
  39.                 break;
  40.             do {
  41.                 barrier();
  42.             } while (p->has_cpu);
  43.         }
  44. #endif
  45.         free_uid(p);
  46.         nr_tasks--;
  47.         add_free_taskslot(p->tarray_ptr);
  48.  
  49.         write_lock_irq(&tasklist_lock);
  50.         unhash_pid(p);
  51.         REMOVE_LINKS(p);
  52.         write_unlock_irq(&tasklist_lock);
  53.  
  54.         release_thread(p);
  55.         current->cmin_flt += p->min_flt + p->cmin_flt;
  56.         current->cmaj_flt += p->maj_flt + p->cmaj_flt;
  57.         current->cnswap += p->nswap + p->cnswap;
  58.         free_task_struct(p);
  59.     } else {
  60.         printk("task releasing itself\n");
  61.     }
  62. }
  63.  
  64. /*
  65.  * This checks not only the pgrp, but falls back on the pid if no
  66.  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
  67.  * without this...
  68.  */
  69. int session_of_pgrp(int pgrp)
  70. {
  71.     struct task_struct *p;
  72.     int fallback;
  73.  
  74.     fallback = -1;
  75.     read_lock(&tasklist_lock);
  76.     for_each_task(p) {
  77.          if (p->session <= 0)
  78.              continue;
  79.         if (p->pgrp == pgrp) {
  80.             fallback = p->session;
  81.             break;
  82.         }
  83.         if (p->pid == pgrp)
  84.             fallback = p->session;
  85.     }
  86.     read_unlock(&tasklist_lock);
  87.     return fallback;
  88. }
  89.  
  90. /*
  91.  * Determine if a process group is "orphaned", according to the POSIX
  92.  * definition in 2.2.2.52.  Orphaned process groups are not to be affected
  93.  * by terminal-generated stop signals.  Newly orphaned process groups are
  94.  * to receive a SIGHUP and a SIGCONT.
  95.  *
  96.  * "I ask you, have you ever known what it is to be an orphan?"
  97.  */
  98. static int will_become_orphaned_pgrp(int pgrp, struct task_struct * ignored_task)
  99. {
  100.     struct task_struct *p;
  101.  
  102.     read_lock(&tasklist_lock);
  103.     for_each_task(p) {
  104.         if ((p == ignored_task) || (p->pgrp != pgrp) ||
  105.             (p->state == TASK_ZOMBIE) ||
  106.             (p->p_pptr->pid == 1))
  107.             continue;
  108.         if ((p->p_pptr->pgrp != pgrp) &&
  109.             (p->p_pptr->session == p->session)) {
  110.             read_unlock(&tasklist_lock);
  111.              return 0;
  112.         }
  113.     }
  114.     read_unlock(&tasklist_lock);
  115.     return 1;    /* (sighing) "Often!" */
  116. }
  117.  
  118. int is_orphaned_pgrp(int pgrp)
  119. {
  120.     return will_become_orphaned_pgrp(pgrp, 0);
  121. }
  122.  
  123. static inline int has_stopped_jobs(int pgrp)
  124. {
  125.     int retval = 0;
  126.     struct task_struct * p;
  127.  
  128.     read_lock(&tasklist_lock);
  129.     for_each_task(p) {
  130.         if (p->pgrp != pgrp)
  131.             continue;
  132.         if (p->state != TASK_STOPPED)
  133.             continue;
  134.         retval = 1;
  135.         break;
  136.     }
  137.     read_unlock(&tasklist_lock);
  138.     return retval;
  139. }
  140.  
  141. static inline void forget_original_parent(struct task_struct * father)
  142. {
  143.     struct task_struct * p;
  144.  
  145.     read_lock(&tasklist_lock);
  146.     for_each_task(p) {
  147.         if (p->p_opptr == father) {
  148.             p->exit_signal = SIGCHLD;
  149.             p->p_opptr = child_reaper; /* init */
  150.             if (p->pdeath_signal) send_sig(p->pdeath_signal, p, 0);
  151.         }
  152.     }
  153.     read_unlock(&tasklist_lock);
  154. }
  155.  
  156. static inline void close_files(struct files_struct * files)
  157. {
  158.     int i, j;
  159.  
  160.     j = 0;
  161.     for (;;) {
  162.         unsigned long set = files->open_fds.fds_bits[j];
  163.         i = j * __NFDBITS;
  164.         j++;
  165.         if (i >= files->max_fds)
  166.             break;
  167.         while (set) {
  168.             if (set & 1) {
  169.                 struct file * file = files->fd[i];
  170.                 if (file) {
  171.                     files->fd[i] = NULL;
  172.                     filp_close(file, files);
  173.                 }
  174.             }
  175.             i++;
  176.             set >>= 1;
  177.         }
  178.     }
  179. }
  180.  
  181. extern kmem_cache_t *files_cachep;  
  182.  
  183. static inline void __exit_files(struct task_struct *tsk)
  184. {
  185.     struct files_struct * files = tsk->files;
  186.  
  187.     if (files) {
  188.         tsk->files = NULL;
  189.         if (atomic_dec_and_test(&files->count)) {
  190.             close_files(files);
  191.             /*
  192.              * Free the fd array as appropriate ...
  193.              */
  194.             if (NR_OPEN * sizeof(struct file *) == PAGE_SIZE)
  195.                 free_page((unsigned long) files->fd);
  196.             else
  197.                 kfree(files->fd);
  198.             kmem_cache_free(files_cachep, files);
  199.         }
  200.     }
  201. }
  202.  
  203. void exit_files(struct task_struct *tsk)
  204. {
  205.     __exit_files(tsk);
  206. }
  207.  
  208. static inline void __exit_fs(struct task_struct *tsk)
  209. {
  210.     struct fs_struct * fs = tsk->fs;
  211.  
  212.     if (fs) {
  213.         tsk->fs = NULL;
  214.         if (atomic_dec_and_test(&fs->count)) {
  215.             dput(fs->root);
  216.             dput(fs->pwd);
  217.             kfree(fs);
  218.         }
  219.     }
  220. }
  221.  
  222. void exit_fs(struct task_struct *tsk)
  223. {
  224.     __exit_fs(tsk);
  225. }
  226.  
  227. static inline void __exit_sighand(struct task_struct *tsk)
  228. {
  229.     struct signal_struct * sig = tsk->sig;
  230.  
  231.     if (sig) {
  232.         unsigned long flags;
  233.  
  234.         spin_lock_irqsave(&tsk->sigmask_lock, flags);
  235.         tsk->sig = NULL;
  236.         spin_unlock_irqrestore(&tsk->sigmask_lock, flags);
  237.         if (atomic_dec_and_test(&sig->count))
  238.             kfree(sig);
  239.     }
  240.  
  241.     flush_signals(tsk);
  242. }
  243.  
  244. void exit_sighand(struct task_struct *tsk)
  245. {
  246.     __exit_sighand(tsk);
  247. }
  248.  
  249. static inline void __exit_mm(struct task_struct * tsk)
  250. {
  251.     struct mm_struct * mm = tsk->mm;
  252.  
  253.     /* Set us up to use the kernel mm state */
  254.     if (mm != &init_mm) {
  255.         flush_cache_mm(mm);
  256.         flush_tlb_mm(mm);
  257.         destroy_context(mm);
  258.         tsk->mm = &init_mm;
  259.         tsk->swappable = 0;
  260.         SET_PAGE_DIR(tsk, swapper_pg_dir);
  261.         mm_release();
  262.         mmput(mm);
  263.     }
  264. }
  265.  
  266. void exit_mm(struct task_struct *tsk)
  267. {
  268.     __exit_mm(tsk);
  269. }
  270.  
  271. /*
  272.  * Send signals to all our closest relatives so that they know
  273.  * to properly mourn us..
  274.  */
  275. static void exit_notify(void)
  276. {
  277.     struct task_struct * p;
  278.  
  279.     forget_original_parent(current);
  280.     /*
  281.      * Check to see if any process groups have become orphaned
  282.      * as a result of our exiting, and if they have any stopped
  283.      * jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
  284.      *
  285.      * Case i: Our father is in a different pgrp than we are
  286.      * and we were the only connection outside, so our pgrp
  287.      * is about to become orphaned.
  288.      */
  289.     if ((current->p_pptr->pgrp != current->pgrp) &&
  290.         (current->p_pptr->session == current->session) &&
  291.         will_become_orphaned_pgrp(current->pgrp, current) &&
  292.         has_stopped_jobs(current->pgrp)) {
  293.         kill_pg(current->pgrp,SIGHUP,1);
  294.         kill_pg(current->pgrp,SIGCONT,1);
  295.     }
  296.  
  297.     /* Let father know we died */
  298.     notify_parent(current, current->exit_signal);
  299.  
  300.     /*
  301.      * This loop does two things:
  302.      *
  303.        * A.  Make init inherit all the child processes
  304.      * B.  Check to see if any process groups have become orphaned
  305.      *    as a result of our exiting, and if they have any stopped
  306.      *    jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
  307.      */
  308.  
  309.     write_lock_irq(&tasklist_lock);
  310.     while (current->p_cptr != NULL) {
  311.         p = current->p_cptr;
  312.         current->p_cptr = p->p_osptr;
  313.         p->p_ysptr = NULL;
  314.         p->flags &= ~(PF_PTRACED|PF_TRACESYS);
  315.  
  316.         p->p_pptr = p->p_opptr;
  317.         p->p_osptr = p->p_pptr->p_cptr;
  318.         if (p->p_osptr)
  319.             p->p_osptr->p_ysptr = p;
  320.         p->p_pptr->p_cptr = p;
  321.         if (p->state == TASK_ZOMBIE)
  322.             notify_parent(p, p->exit_signal);
  323.         /*
  324.          * process group orphan check
  325.          * Case ii: Our child is in a different pgrp
  326.          * than we are, and it was the only connection
  327.          * outside, so the child pgrp is now orphaned.
  328.          */
  329.         if ((p->pgrp != current->pgrp) &&
  330.             (p->session == current->session)) {
  331.             int pgrp = p->pgrp;
  332.  
  333.             write_unlock_irq(&tasklist_lock);
  334.             if (is_orphaned_pgrp(pgrp) && has_stopped_jobs(pgrp)) {
  335.                 kill_pg(pgrp,SIGHUP,1);
  336.                 kill_pg(pgrp,SIGCONT,1);
  337.             }
  338.             write_lock_irq(&tasklist_lock);
  339.         }
  340.     }
  341.     write_unlock_irq(&tasklist_lock);
  342.  
  343.     if (current->leader)
  344.         disassociate_ctty(1);
  345. }
  346.  
  347. NORET_TYPE void do_exit(long code)
  348. {
  349.     struct task_struct *tsk = current;
  350.  
  351.     if (in_interrupt())
  352.         printk("Aiee, killing interrupt handler\n");
  353.     if (!tsk->pid)
  354.         panic("Attempted to kill the idle task!");
  355.     tsk->flags |= PF_EXITING;
  356.     start_bh_atomic();
  357.     del_timer(&tsk->real_timer);
  358.     end_bh_atomic();
  359.  
  360.     lock_kernel();
  361. fake_volatile:
  362. #ifdef CONFIG_BSD_PROCESS_ACCT
  363.     acct_process(code);
  364. #endif
  365.     sem_exit();
  366.     __exit_mm(tsk);
  367. #if CONFIG_AP1000
  368.     exit_msc(tsk);
  369. #endif
  370.     __exit_files(tsk);
  371.     __exit_fs(tsk);
  372.     __exit_sighand(tsk);
  373.     exit_thread();
  374.     tsk->state = TASK_ZOMBIE;
  375.     tsk->exit_code = code;
  376.     exit_notify();
  377. #ifdef DEBUG_PROC_TREE
  378.     audit_ptree();
  379. #endif
  380.     if (tsk->exec_domain && tsk->exec_domain->module)
  381.         __MOD_DEC_USE_COUNT(tsk->exec_domain->module);
  382.     if (tsk->binfmt && tsk->binfmt->module)
  383.         __MOD_DEC_USE_COUNT(tsk->binfmt->module);
  384.     schedule();
  385. /*
  386.  * In order to get rid of the "volatile function does return" message
  387.  * I did this little loop that confuses gcc to think do_exit really
  388.  * is volatile. In fact it's schedule() that is volatile in some
  389.  * circumstances: when current->state = ZOMBIE, schedule() never
  390.  * returns.
  391.  *
  392.  * In fact the natural way to do all this is to have the label and the
  393.  * goto right after each other, but I put the fake_volatile label at
  394.  * the start of the function just in case something /really/ bad
  395.  * happens, and the schedule returns. This way we can try again. I'm
  396.  * not paranoid: it's just that everybody is out to get me.
  397.  */
  398.     goto fake_volatile;
  399. }
  400.  
  401. asmlinkage int sys_exit(int error_code)
  402. {
  403.     do_exit((error_code&0xff)<<8);
  404. }
  405.  
  406. asmlinkage int sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
  407. {
  408.     int flag, retval;
  409.     struct wait_queue wait = { current, NULL };
  410.     struct task_struct *p;
  411.  
  412.     if (options & ~(WNOHANG|WUNTRACED|__WCLONE))
  413.         return -EINVAL;
  414.  
  415.     add_wait_queue(¤t->wait_chldexit,&wait);
  416. repeat:
  417.     flag = 0;
  418.     read_lock(&tasklist_lock);
  419.      for (p = current->p_cptr ; p ; p = p->p_osptr) {
  420.         if (pid>0) {
  421.             if (p->pid != pid)
  422.                 continue;
  423.         } else if (!pid) {
  424.             if (p->pgrp != current->pgrp)
  425.                 continue;
  426.         } else if (pid != -1) {
  427.             if (p->pgrp != -pid)
  428.                 continue;
  429.         }
  430.         /* wait for cloned processes iff the __WCLONE flag is set */
  431.         if ((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
  432.             continue;
  433.         flag = 1;
  434.         switch (p->state) {
  435.             case TASK_STOPPED:
  436.                 if (!p->exit_code)
  437.                     continue;
  438.                 if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED))
  439.                     continue;
  440.                 read_unlock(&tasklist_lock);
  441.                 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; 
  442.                 if (!retval && stat_addr) 
  443.                     retval = put_user((p->exit_code << 8) | 0x7f, stat_addr);
  444.                 if (!retval) {
  445.                     p->exit_code = 0;
  446.                     retval = p->pid;
  447.                 }
  448.                 goto end_wait4;
  449.             case TASK_ZOMBIE:
  450.                 current->times.tms_cutime += p->times.tms_utime + p->times.tms_cutime;
  451.                 current->times.tms_cstime += p->times.tms_stime + p->times.tms_cstime;
  452.                 read_unlock(&tasklist_lock);
  453.                 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  454.                 if (!retval && stat_addr)
  455.                     retval = put_user(p->exit_code, stat_addr);
  456.                 if (retval)
  457.                     goto end_wait4; 
  458.                 retval = p->pid;
  459.                 if (p->p_opptr != p->p_pptr) {
  460.                     write_lock_irq(&tasklist_lock);
  461.                     REMOVE_LINKS(p);
  462.                     p->p_pptr = p->p_opptr;
  463.                     SET_LINKS(p);
  464.                     write_unlock_irq(&tasklist_lock);
  465.                     notify_parent(p, SIGCHLD);
  466.                 } else
  467.                     release(p);
  468. #ifdef DEBUG_PROC_TREE
  469.                 audit_ptree();
  470. #endif
  471.                 goto end_wait4;
  472.             default:
  473.                 continue;
  474.         }
  475.     }
  476.     read_unlock(&tasklist_lock);
  477.     if (flag) {
  478.         retval = 0;
  479.         if (options & WNOHANG)
  480.             goto end_wait4;
  481.         retval = -ERESTARTSYS;
  482.         if (signal_pending(current))
  483.             goto end_wait4;
  484.         current->state=TASK_INTERRUPTIBLE;
  485.         schedule();
  486.         goto repeat;
  487.     }
  488.     retval = -ECHILD;
  489. end_wait4:
  490.     remove_wait_queue(¤t->wait_chldexit,&wait);
  491.     return retval;
  492. }
  493.  
  494. #ifndef __alpha__
  495.  
  496. /*
  497.  * sys_waitpid() remains for compatibility. waitpid() should be
  498.  * implemented by calling sys_wait4() from libc.a.
  499.  */
  500. asmlinkage int sys_waitpid(pid_t pid,unsigned int * stat_addr, int options)
  501. {
  502.     return sys_wait4(pid, stat_addr, options, NULL);
  503. }
  504.  
  505. #endif
  506.